package cn.sxt.logistics.controller;


import cn.sxt.logistics.pojo.*;
import cn.sxt.logistics.service.IAdminService;
import cn.sxt.logistics.service.IRolePermissionService;
import cn.sxt.logistics.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-13
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRolePermissionService rolePermissionService;


    @Autowired
    private IAdminService adminService;

    @GetMapping("/roleManager")
    public String roleManager(){

        return "roleManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(
            @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize
            , String keyword){

        //分页对象
        Page<Role> adminPage = new Page<Role>(pageNum,pageSize);
        //条件对象
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(keyword)){
            queryWrapper.like("rolename",keyword);
        }
        //查询结果
        Page<Role> result = roleService.page(adminPage, queryWrapper);

        return new ResultObject(result);
    }

    //删除功能
    @GetMapping("/delete")
    @ResponseBody
    public MessageObject delete(Long roleId){

        /**
         * 删除之前，判断当前角色是否还有管理员拥有此角色
         * 有:不能删除
         * 没有:可以删除
         *
         */

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<Admin> admins = adminService.list(queryWrapper);
        List<String> usernames = new ArrayList<>();
        for (Admin admin : admins) {
            usernames.add(admin.getUsername());
        }

        if(admins.size()>0){

            return new MessageObject(0, "当前还拥有管理员"+usernames+"，不能删除删除！");

        }


        boolean flag = roleService.removeById(roleId);

        MessageObject mo = new MessageObject(1, "删除数据成功");
        if(!flag){
            mo = new MessageObject(0,"删除数据失败，请联系管理员");
        }
        return mo;
    }


    /*跳转到编辑页面*/
    @GetMapping("/roleEdit")
    public String roleEdit(Model m, Long roleId){

        /*修改*/
        if(roleId !=null){
            Role role = roleService.getById(roleId);
            m.addAttribute("role",role);

            /*查询出角色权限表中的所有的权限id*/
            QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",roleId);
            List<RolePermission> rolePermissions = rolePermissionService.list(wrapper);

            StringBuilder permissionIds = new StringBuilder();
            if(rolePermissions.size()>1){
                for (RolePermission rolePermission : rolePermissions) {
                    permissionIds.append(rolePermission.getPermissionId()+",");
                }
                //删除最后一个多余的逗号
                permissionIds.deleteCharAt(permissionIds.length()-1);
            }

            m.addAttribute("permissionIds",permissionIds);
        }

        return "roleEdit";
    }



    /*新增数据*/
    @PostMapping("/insert")
    @ResponseBody
    public MessageObject insert(Role role,@RequestParam Long[] permissionIds){

        System.out.println("permissionIds :"+ Arrays.asList(permissionIds));

        //新增管理员
        boolean flag = roleService.saveOrUpdate(role,permissionIds);


        MessageObject mo = new MessageObject(1, "新增数据成功");
        if(!flag){
            mo = new MessageObject(0,"新增数据失败，请联系管理员");
        }
        return mo;
    }



    /*修改数据*/
    @PostMapping("/update")
    @ResponseBody
    public MessageObject update(Role role,@RequestParam Long[] permissionIds){
        System.out.println("permissionIds :"+ Arrays.asList(permissionIds));

        //修改管理员
        boolean flag = roleService.saveOrUpdate(role,permissionIds);
        MessageObject mo = new MessageObject(1, "修改数据成功");
        if(!flag){
            mo = new MessageObject(0,"修改数据失败，请联系管理员");
        }
        return mo;
    }

}

